package health_system.health_system.dto;

import health_system.health_system.entity.Friendship;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FriendshipDTO {
    private Long id;
    private UserDTO requester;
    private UserDTO addressee;
    private Friendship.FriendshipStatus status;
    private LocalDateTime acceptedAt;
    private String requesterNote;
    private String addresseeNote;
    private String friendCircle;
    private PrivacySettingsDTO privacySettings;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    
    // Client-specific field, not stored in database
    private String requestRemark;
    
    public static FriendshipDTO fromEntity(Friendship friendship) {
        if (friendship == null) {
            return null;
        }
        
        return FriendshipDTO.builder()
                .id(friendship.getId())
                .requester(UserDTO.fromEntityBasic(friendship.getRequester()))
                .addressee(UserDTO.fromEntityBasic(friendship.getAddressee()))
                .status(friendship.getStatus())
                .acceptedAt(friendship.getAcceptedAt())
                .requesterNote(friendship.getRequesterNote())
                .addresseeNote(friendship.getAddresseeNote())
                .friendCircle(friendship.getFriendCircle())
                .privacySettings(PrivacySettingsDTO.fromEntity(friendship.getPrivacySettings()))
                .createdAt(friendship.getCreatedAt())
                .updatedAt(friendship.getUpdatedAt())
                .build();
    }
    
    public static FriendshipDTO fromEntityWithUsers(Friendship friendship) {
        if (friendship == null) {
            return null;
        }
        
        return FriendshipDTO.builder()
                .id(friendship.getId())
                .requester(UserDTO.fromEntity(friendship.getRequester()))
                .addressee(UserDTO.fromEntity(friendship.getAddressee()))
                .status(friendship.getStatus())
                .acceptedAt(friendship.getAcceptedAt())
                .requesterNote(friendship.getRequesterNote())
                .addresseeNote(friendship.getAddresseeNote())
                .friendCircle(friendship.getFriendCircle())
                .privacySettings(PrivacySettingsDTO.fromEntity(friendship.getPrivacySettings()))
                .createdAt(friendship.getCreatedAt())
                .updatedAt(friendship.getUpdatedAt())
                .build();
    }

    @Override
    public String toString() {
        return "FriendshipDTO{" +
                "id=" + id +
                ", requester=" + requester +
                ", addressee=" + addressee +
                ", status=" + status +
                ", acceptedAt=" + acceptedAt +
                ", requesterNote='" + requesterNote + '\'' +
                ", addresseeNote='" + addresseeNote + '\'' +
                ", friendCircle='" + friendCircle + '\'' +
                ", privacySettings=" + privacySettings +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                ", requestRemark='" + requestRemark + '\'' +
                '}';
    }
}